<template>
  <view :style="colorStyle">
    <view
      v-if="detail.statusText !== '已结束'"
      class="spikeModule"
      :style="{
        paddingTop: `${info.style.pTop * 2}rpx`,
        paddingLeft: `${info.style.pLeft * 2}rpx`,
        paddingRight: `${info.style.pRight * 2}rpx`,
        paddingBottom: `${info.style.pBottom * 2}rpx`,
        marginTop: `${info.style.mTop * 2}rpx`,
        marginLeft: `${info.style.mLeft * 2}rpx`,
        marginRight: `${info.style.mRight * 2}rpx`,
        marginBottom: `${info.style.mBottom * 2}rpx`,
        borderRadius: `${info.radius * 2}rpx`,
        background: `${info.bgColor}`,
      }"
    >
      <view
        class="info2"
        :style="{
          background: `${
            info.colortem == 1
              ? 'linear-gradient(180deg, var(--view-op-ten024) 0%, var(--view-op-ten0) 100%)'
              : 'linear-gradient(180deg, ' +
                info.titleBgColor1 +
                ' 0%, ' +
                info.titleBgColor2 +
                ' 100%)'
          }`,
        }"
      ></view>
      <view class="info1">
        <view class="titlebox flex aitems between">
          <view
            class="title flex aitems"
            :style="{ color: `${info.titleColor}` }"
          >
            <view>
              {{ detail.title || "" }}
            </view>
            <view class="timebox flex aitems" v-if="info.iskill">
              <u-count-down
                :time="downTime"
                format="HH:mm:ss"
                autoStart
                millisecond
                @change="downonChange"
                @finish="downfinish"
              >
                <view class="flex aitems">
                  <view
                    class="time"
                    :style="{
                      background: `${
                        info.colortem == 1 ? 'var(--view-theme)' : info.color
                      }`,
                    }"
                  >
                    <text class="">{{
                      timeData.hours > 10
                        ? timeData.hours
                        : "0" + timeData.hours
                    }}</text>
                  </view>
                  <text
                    class="time1"
                    :style="{
                      color: `${
                        info.colortem == 1 ? 'var(--view-theme)' : info.color
                      }`,
                    }"
                    >:</text
                  >
                  <view
                    class="time"
                    :style="{
                      background: `${
                        info.colortem == 1 ? 'var(--view-theme)' : info.color
                      }`,
                    }"
                  >
                    <text class="">{{
                      timeData.minutes > 10
                        ? timeData.minutes
                        : "0" + timeData.minutes
                    }}</text>
                  </view>
                  <text
                    class="time1"
                    :style="{
                      color: `${
                        info.colortem == 1 ? 'var(--view-theme)' : info.color
                      }`,
                    }"
                    >:</text
                  >
                  <view
                    class="time"
                    :style="{
                      background: `${
                        info.colortem == 1 ? 'var(--view-theme)' : info.color
                      }`,
                    }"
                  >
                    <text class="">{{
                      timeData.seconds > 10
                        ? timeData.seconds
                        : "0" + timeData.seconds
                    }}</text>
                  </view>
                </view>
              </u-count-down>
              <!-- <view
                class="time"
                :style="{
                  background: `${
                    info.colortem == 1 ? 'var(--view-theme)' : info.color
                  }`,
                }"
                >08</view
              >
              <view
                class="time1"
                :style="{
                  color: `${
                    info.colortem == 1 ? 'var(--view-theme)' : info.color
                  }`,
                }"
                >:
              </view>
              <view
                class="time"
                :style="{
                  background: `${
                    info.colortem == 1 ? 'var(--view-theme)' : info.color
                  }`,
                }"
                >08</view
              >
              <view
                class="time1"
                :style="{
                  color: `${
                    info.colortem == 1 ? 'var(--view-theme)' : info.color
                  }`,
                }"
                >:
              </view>
              <view
                class="time"
                :style="{
                  background: `${
                    info.colortem == 1 ? 'var(--view-theme)' : info.color
                  }`,
                }"
                >08</view
              > -->
            </view>
          </view>
          <view
            class="more flex aitems"
            :style="{ color: `${info.titleColor}` }"
            v-if="info.moreShow == 1 && detail.type == 'limitTimeSecKill'"
            @click="gourl(`/pages/activities/spike?id=${info.value[0].id}`)"
          >
            <span>{{ info.more }}</span>
            <u-icon
              name="arrow-right"
              :color="`${info.titleColor}`"
              size="12"
            ></u-icon>
          </view>
          <view
            class="more flex aitems"
            :style="{ color: `${info.titleColor}` }"
            v-if="info.moreShow == 1 && detail.type == 'limitTimeDiscount'"
            @click="gourl(`/pages/activities/discount?id=${info.value[0].id}`)"
          >
            <span>{{ info.more }}</span>
            <u-icon
              name="arrow-right"
              :color="`${info.titleColor}`"
              size="12"
            ></u-icon>
          </view>
        </view>

        <view class="sellerbox">
          <view
            class="sellerList"
            v-for="(q, index, key) in list"
            :key="key"
            @click="gourl(`/pages/goods/detail?id=${q.goodsId}`)"
          >
            <view class="sellerinfo1 flex aitems">
              <image
                class="sellerLogo"
                :src="q.goodsImg"
                alt=""
                mode="aspectFill"
              />
              <view class="sellerNameBox">
                <view class="sellerName ellipsis1">{{ q.goodsName }}</view>
                <view
                  v-if="q.publicityType == 'limitTimeSecKill'"
                  class="pricebox flex aitems"
                  :style="{
                    color: `${
                      info.colortem == 1 ? 'var(--view-theme)' : info.color
                    }`,
                  }"
                >
                  <view class="unit">￥</view>
                  <view class="num">{{ q.config.detailList[0].price }}</view>
                </view>
                <view
                  v-if="q.publicityType == 'limitTimeDiscount'"
                  class="pricebox flex aitems"
                  :style="{
                    color: `${
                      info.colortem == 1 ? 'var(--view-theme)' : info.color
                    }`,
                  }"
                >
                  <view class="unit">￥</view>
                  <view class="num" v-if="q.config.type == 3">
                    {{ Number(q.config.detailList[0].price).toFixed(2) }}</view
                  >
                  <view class="num" v-if="q.config.type == 2">
                    {{
                      Number(
                        q.goodsPrice - q.config.detailList[0].price
                      ).toFixed(2)
                    }}</view
                  >
                  <view class="num" v-if="q.config.type == 1">{{
                    Number(
                      (q.goodsPrice * q.config.detailList[0].price) / 10
                    ).toFixed(2)
                  }}</view>
                </view>
                <view
                  class="jindubox flex between"
                  :style="{
                    background: `${
                      info.colortem == 1
                        ? 'var(--view-minorColorT)'
                        : hexToRgb(info.color, 0.1)
                    }`,
                  }"
                >
                  <view class="left flex aitems">
                    <view class="jindu">
                      <view
                        class="value"
                        :style="{
                          background: `${
                            info.colortem == 1
                              ? 'var(--view-theme)'
                              : info.color
                          }`,
                          width: `${q.percentage}%`,
                        }"
                      >
                      </view>
                    </view>
                    <view
                      class="shengyu"
                      :style="{
                        color: `${
                          info.colortem == 1 ? 'var(--view-theme)' : info.color
                        }`,
                      }"
                    >
                      剩余{{ Number(q.goodsStock - q.salesVolume).toFixed(0) }}
                    </view>
                  </view>
                  <view
                    class="right"
                    :style="{
                      background: `linear-gradient(270deg,${
                        info.colortem == 1
                          ? 'var(--view-op-ten6)'
                          : hexToRgb(info.color, 0.6)
                      } 0.96%,${
                        info.colortem == 1 ? 'var(--view-theme)' : info.color
                      } 97.01%)`,
                    }"
                  >
                    抢
                  </view>
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { HTTP_STATUS } from "../../../servers/config";
import colors from "@/mixins/color";
import { publicityInfo, publicityGoodsPage } from "@/servers/servers.js";
import { go } from "../../../utils/wxAPI";
export default {
  mixins: [colors],
  props: {
    info: {
      default: {},
    },
  },
  data() {
    return {
      detail: {},
      list: [],
      timeData: {},
      downTime: 99999999,
    };
  },
  async mounted() {
    if (this.info.value.length !== 0) {
      await this.publicityInfo();
    }
  },
  methods: {
    async publicityInfo() {
      let { data } = await publicityInfo({
        id: this.info.value[0].id,
      });
      if (data.code == HTTP_STATUS.RUSELT_SUCCESS) {
        let info = data.data;
        let now = Date.now();
        info.config = info.config ? JSON.parse(info.config) : {};
        let endTime = new Date(info.endTime.replace(/-/g, "/")).getTime();
        let beginTime = new Date(info.beginTime.replace(/-/g, "/")).getTime();

        if (now < beginTime) {
          info.statusText = "即将开始";
          this.downTime = beginTime - now;
        } else if (endTime - now > 0) {
          info.statusText = "抢购中";
          this.downTime = endTime - now;
        } else {
          info.statusText = "已结束";
          this.downTime = 0;
        }
        if (info.statusText !== "已结束") {
          this.publicityGoodsPage();
        }
        this.detail = info;
      }
    },
    async publicityGoodsPage() {
      let { data } = await publicityGoodsPage({
        current: 1,
        size: 2,
        publicityId: this.info.value[0].id,
      });
      if (data.code == HTTP_STATUS.RUSELT_SUCCESS) {
        let list = data.data.records;
        list.map((item) => {
          //数据处理
          item.config = item.config ? JSON.parse(item.config) : [];
          item.goodsStock = 0;
          item.salesVolume = 0;
          item.config.detailList.map((items) => {
            item.goodsStock += items.stock;
            item.salesVolume += items.salesVolume;
          });

          let percentage = Number(item.salesVolume / item.goodsStock) * 100;
          item.percentage =
            Number(percentage) > 100 ? 100 : Number(percentage).toFixed(0);
        });
        this.list = list;
      }
    },
    downonChange(e) {
      let hours = e.hours + e.days * 24;
      e.hours = hours;
      this.timeData = e;
    },

    downfinish() {
      this.publicityInfo();
    },
    hexToRgb(hex, o) {
      console.log(hex);
      // 移除十六进制颜色中的'#'
      let sanitizedHex = hex.replace("#", "");

      // 解析红、绿、蓝色值
      let r = parseInt(sanitizedHex.substring(0, 2), 16);
      let g = parseInt(sanitizedHex.substring(2, 4), 16);
      let b = parseInt(sanitizedHex.substring(4, 6), 16);

      return `rgb(${r}, ${g}, ${b},${o})`;
    },

    gourl(url) {
      go(url);
    },
  },
};
</script>

<style lang="scss" scoped>
.spikeModule {
  position: relative;
  overflow: hidden;

  .info1 {
    position: relative;
    z-index: 1;

    .titlebox {
      .title {
        font-weight: bold;
        font-size: 36rpx;

        .timebox {
          margin-left: 16rpx;

          .time {
            border-radius: 8rpx;
            font-size: 24rpx;
            color: #fff;
            background: #f65b49;
            width: 50rpx;
            height: 50rpx;
            line-height: 50rpx;
            text-align: center;
          }

          .time1 {
            color: #f65b49;
            font-size: 24rpx;
            font-weight: bold;
            margin: 0 8rpx;
          }
        }
      }

      .more {
        color: #1f1f1f;
        font-size: 24rpx;
      }
    }

    .sellerbox {
      .sellerList {
        position: relative;
        margin: 32rpx 0 0;
        overflow: hidden;

        .sellerinfo1 {
          position: relative;
          z-index: 1;

          .sellerLogo {
            width: 220rpx;
            height: 220rpx;
            border-radius: 28rpx;
            object-fit: cover;
          }

          .sellerNameBox {
            width: calc(100% - 20rpx - 220rpx);
            margin-left: 20rpx;

            .sellerName {
              color: #1f1f1f;
              font-size: 28rpx;
              font-weight: bold;
            }

            .pricebox {
              color: #f65b49;
              margin: 24rpx 0 0;
              font-weight: bold;

              .unit {
                font-size: 32rpx;
                position: relative;
                top: 8rpx;
              }

              .num {
                font-size: 44rpx;
              }
            }

            .jindubox {
              border-radius: 96rpx;
              height: 76rpx;
              margin: 20rpx 0 0;
              background: #ffedeb;
              width: 406rpx;
              overflow: hidden;

              .left {
                padding: 0 25rpx;

                .jindu {
                  width: 120rpx;
                  height: 16rpx;
                  border-radius: 56rpx;
                  background: #fff;

                  .value {
                    width: 50%;
                    height: 100%;
                    background: #f65b49;
                    border-radius: 56rpx;
                  }
                }

                .shengyu {
                  color: #f65b49;
                  font-size: 22rpx;
                  margin-left: 14rpx;
                }
              }

              .right {
                width: 128rpx;
                height: 100%;
                text-align: center;
                font-size: 40rpx;
                line-height: 76rpx;
                color: #fff;
                font-weight: bold;
                position: relative;
                right: -8rpx;
                background: linear-gradient(
                  270deg,
                  #ff9285 0.96%,
                  #f65b49 97.01%
                );
              }
            }
          }
        }
      }
    }
  }

  .info2 {
    height: 172rpx;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
  }
}
</style>
